﻿using SL651Center2.x.Enums;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using TouchSocket.Core;

namespace SL651Center2.x.Messages
{
    /// <summary>
    /// 运行配置
    /// </summary>
    public class MessageRunConfig : IMessage
    {
        /// <summary>
        /// 流水号
        /// </summary>
        public byte[] SerialNumber { get; set; } = Array.Empty<byte>();
        /// <summary>
        /// 流水号数字形式
        /// </summary>
        public ushort SerialNum { get; set; }
        /// <summary>
        /// 发报时间
        /// </summary>
        public byte[] SendTime { get; set; } = Array.Empty<byte>();
        /// <summary>
        /// 发报时间字符串形式
        /// </summary>
        public string SendTimeStr { get; set; } = string.Empty;
        /// <summary>
        /// 遥测站地址
        /// </summary>
        public byte[] TelemetryAddress { get; set; } = Array.Empty<byte>();
        /// <summary>
        /// 遥测站地址的字符串形式
        /// </summary>
        public string TelemetryAddress_str { get; set; } = string.Empty;

        /// <summary>
        /// 时段数据
        /// </summary>
        public TimeData[] TimeDatas { get; private set; } = Array.Empty<TimeData>();

        /// <summary>
        /// 数据列表
        /// </summary>
        public RunConfigData[] RunConfigDatas { get; private set; } = Array.Empty<RunConfigData>();

        public void Read(in ByteBlock byteBlock)
        {
            //流水号
            SerialNumber = byteBlock.ReadToSpan(2).ToArray();
            SerialNum = Convert.ToUInt16(Helper.ToHEXString(SerialNumber), 16);

            //发报时间
            SendTime = byteBlock.ReadToSpan(6).ToArray();
            SendTimeStr = Helper.ByteToBCDTime(SendTime);

            //遥测站地址
            var code = byteBlock.ReadByte();
            byteBlock.ReadByte();
            TelemetryAddress = byteBlock.ReadToSpan(5).ToArray();
            TelemetryAddress_str = Helper.ToHEXString(TelemetryAddress);

            List<RunConfigData> elements = new List<RunConfigData>();

            //读取一个标识符
            var elemCode = byteBlock.ReadByte();
            while (elemCode != 0x03 && elemCode != 0x17)
            {
                //读取数据定义
                var dlen = byteBlock.ReadByte();
                var dalen = dlen >> 3;
                var dalen1 = dlen & 0x07;

                var (d, pre) = RunConfigInfo.GetValueType(elemCode);
                var (name, unit) = RunConfigInfo.Instance.GetInfo((RunConfigCode)elemCode);

                if (d != 0)
                {
                    var db = new byte[d];
                    byteBlock.Read(db);
                    var d1 = Helper.ToHEXString(db);
                    var num1 = Helper.HEXToBCDInt(d1);
                    decimal value;
                    if (Regex.IsMatch(d1, "^[Ff]*$"))
                    {
                        value = 0;
                    }
                    else if (pre != 0)
                    {
                        value = num1 / pre;
                    }
                    else
                    {
                        value = Convert.ToDecimal(num1);
                    }
                    RunConfigData data = new RunConfigData()
                    {
                        Code = (RunConfigCode)elemCode,
                        Name = name,
                        Unit = unit,
                        Value = value,
                    };
                    elements.Add(data);

                }

                //读取下一个要素标识符
                elemCode = byteBlock.ReadByte();

            }

            RunConfigDatas = elements.ToArray();

        }

        public void ReadASCII(in ByteBlock byteBlock)
        {
            //流水号
            SerialNumber = byteBlock.ReadToSpan(4).ToArray();
            SerialNum = Convert.ToUInt16(SerialNumber.ToASCIIString(), 16);

            //发报时间
            SendTime = byteBlock.ReadToSpan(12).ToArray();
            SendTimeStr = Helper.StringToBCDTime(SendTime.ToASCIIString());

            var datab = byteBlock.ToArray(byteBlock.Position);
            var datastr = datab.ToASCIIString();
            var arr = datastr.Split(" ");

            //遥测站地址
            TelemetryAddress_str = arr[1];
            TelemetryAddress = Encoding.ASCII.GetBytes(TelemetryAddress_str);

            List<RunConfigData> elements = new List<RunConfigData>();

            for (int i = 2; i < arr.Length - 1; i++)
            {
                //读取一个标识符
                var elemCode = arr[i];
                var ec = byte.Parse(elemCode, NumberStyles.HexNumber);
                var (name, unit) = RunConfigInfo.Instance.GetInfo((RunConfigCode)ec);
                var d1 = arr[++i];
                decimal value = Convert.ToDecimal(d1);

                RunConfigData data = new RunConfigData()
                {
                    Code = (RunConfigCode)ec,
                    Name = name,
                    Unit = unit,
                    Value = value,
                };
                elements.Add(data);
            }

            RunConfigDatas = elements.ToArray();

        }

    }
}
